#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""  
@Project : wupeiqi_async
@File : 11-异步爬虫.py
@Author : Royce
@Time : 2025/7/13 16:40  
@脚本说明 :
用异步编程来写爬虫
"""
import asyncio
import sys

from util.util import timer

import aiohttp


async def fetch(session, url):
    print("发送请求：", url)
    async with session.get(url, verify_ssl=False) as response:
        text = await response.text()
        print("得到结果：", url, len(text))


@timer('ms')
async def main():
    async with aiohttp.ClientSession() as session:
        # 列出url的列表
        url_list = ['https://python.org', 'https://www.baidu.com', 'https://www.pythonav.com']

        # 创建任务列表
        tasks = [asyncio.create_task(fetch(session, url)) for url in url_list]

        # 将任务列表放入事件循环，并等待它执行完
        await asyncio.wait(tasks)


if __name__ == '__main__':

    # 解决Win32平台程序在退出之前去关闭事件循环而发生的Error
    if sys.platform == 'win32':
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

    asyncio.run(main())
